Skip to content

Make get_norm_layer repr test tolerant of PyTorch bias= field#8880

Merged
ericspod merged 1 commit into
Project-MONAI:devfrom
BRAINSia:norm-layer-repr-pytorch-compat
May 28, 2026
Merged

Make get_norm_layer repr test tolerant of PyTorch bias= field#8880
ericspod merged 1 commit into
Project-MONAI:devfrom
BRAINSia:norm-layer-repr-pytorch-compat

Conversation

@hjmjohnson
Copy link
Copy Markdown
Contributor

PyTorch >= 2.13 adds an optional 'bias=' token to GroupNorm/InstanceNorm repr, breaking the exact-string match in test_norm_layer. Normalize the repr by stripping the bias= field so the test passes on PyTorch versions with or without it (backward- and forward-compatible).

Description

test_norm_layer in tests/networks/layers/test_get_layers.py asserts that a norm layer's repr() exactly equals a hard-coded string. PyTorch >= 2.13 added an optional bias= token to GroupNorm /
InstanceNorm repr, which breaks that exact-string match. This normalizes both the actual and expected repr by stripping the bias= field, so the test passes on PyTorch versions with or without
it — backward- and forward-compatible, with no change to production code.

Types of changes

  • Non-breaking change (fix that would not break existing functionality).
  • Quick tests passed locally (python -m tests.networks.layers.test_get_layers — 7/7).

PyTorch < 2.13: GroupNorm(1, 1, eps=1e-05, affine=True)
PyTorch >= 2.13: GroupNorm(1, 1, eps=1e-05, affine=True, bias=True)

The fix strips , bias=(True|False) from both sides before comparison. Local checks: targeted test 7/7 OK; black/isort/ruff clean; full pre-commit hooks pass.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 27, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: fb5ee8e3-e55a-47e8-9ce6-9b61120f2564

📥 Commits

Reviewing files that changed from the base of the PR and between c80fc22 and 19d7f35.

📒 Files selected for processing (1)
  • tests/networks/layers/test_get_layers.py
🚧 Files skipped from review as they are similar to previous changes (1)
  • tests/networks/layers/test_get_layers.py

📝 Walkthrough

Walkthrough

This PR updates a test file to handle PyTorch version differences in norm-layer string representations. It adds a _strip_bias_field() helper function using regex to remove optional bias=... fragments from layer repr strings, then applies this normalization to the norm-layer test assertion for stable comparisons across PyTorch versions.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed Title concisely describes the main change: making a norm-layer repr test compatible with PyTorch's bias field.
Description check ✅ Passed Description covers the issue, solution, impact, and test results, though formatting is non-standard and some template sections are incomplete.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
tests/networks/layers/test_get_layers.py (1)

22-24: ⚡ Quick win

Add a Google-style docstring to the new helper.

_strip_bias_field is a new definition and should document args/returns in Google style per repo rules.

Proposed patch
 def _strip_bias_field(text: str) -> str:
-    # PyTorch >= 2.13 adds an optional ``bias=`` field to norm-layer repr; drop it for version stability.
+    """Remove optional PyTorch norm-layer ``bias=...`` repr fragment.
+
+    Args:
+        text: Layer string representation to normalize.
+
+    Returns:
+        The normalized representation with optional `, bias=True|False` removed.
+
+    Raises:
+        None.
+    """
     return re.sub(r",\s*bias=(?:True|False)", "", text)

As per coding guidelines: “Docstrings should be present for all definition which describe each variable, return value, and raised exception in the appropriate section of the Google-style of docstrings.”

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tests/networks/layers/test_get_layers.py` around lines 22 - 24, Add a
Google-style docstring to the helper function _strip_bias_field describing its
purpose, its arguments, and return value: explain that it accepts text (str) and
returns a str with any optional ", bias=True" or ", bias=False" substrings
removed for PyTorch repr stability; include an Args section listing text: str
and a Returns section describing the cleaned string; no exceptions need to be
documented.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@tests/networks/layers/test_get_layers.py`:
- Around line 22-24: Add a Google-style docstring to the helper function
_strip_bias_field describing its purpose, its arguments, and return value:
explain that it accepts text (str) and returns a str with any optional ",
bias=True" or ", bias=False" substrings removed for PyTorch repr stability;
include an Args section listing text: str and a Returns section describing the
cleaned string; no exceptions need to be documented.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: aa65659f-2616-4b76-b9ae-2a04645ce360

📥 Commits

Reviewing files that changed from the base of the PR and between e33941c and c80fc22.

📒 Files selected for processing (1)
  • tests/networks/layers/test_get_layers.py

PyTorch >= 2.13 adds an optional 'bias=' token to GroupNorm/InstanceNorm
__repr__, breaking the exact-string match in test_norm_layer. Normalize
the repr by stripping the bias= field so the test passes on PyTorch
versions with or without it (backward- and forward-compatible).

Signed-off-by: Hans Johnson <hans-johnson@uiowa.edu>
@hjmjohnson hjmjohnson force-pushed the norm-layer-repr-pytorch-compat branch from c80fc22 to 19d7f35 Compare May 28, 2026 11:35
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 28, 2026

Actionable comments posted: 0

Copy link
Copy Markdown
Member

@ericspod ericspod left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @hjmjohnson thanks for this fix. I can look into enabling PyTorch 2.13 testing later, I know this was one problem encountered but there might have been others.

@ericspod ericspod merged commit dc0f59c into Project-MONAI:dev May 28, 2026
21 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants